package com.example.jwt.filter;

import com.example.jwt.po.User;
import com.example.jwt.util.JWTUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(urlPatterns = "/testToken", filterName = "jwtFilter")
public class JWTFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        String token = request.getHeader("Authorization");
        User user = new User();
        user.setId("1");
        user.setUsername("hand2020");
        user.setPassword("123456");
        boolean flag = JWTUtil.isVerify(token, user);
        if (flag) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            response.getWriter().write("失败。。。。。。。。");
        }
    }

    @Override
    public void destroy() {

    }
}
